//
//  SJTabbarController.m
//  Recycling
//
//  Created by jzd on 2018/5/16.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "SJTabbarController.h"
#import "HomeViewController.h"
#import "WorkOrderViewController.h"
#import "ControlCenterTableVC.h"
#import "MineViewController.h"
#import "SJNavgationController.h"

@interface SJTabbarController ()

@end

@implementation SJTabbarController
// 只会调用一次
+(void)initialize{
    if (self == [SJTabbarController class]) {
        // 获取哪个类中UITabBarItem
        UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
        // 设置按钮选中标题的颜色:富文本:描述一个文字颜色,字体,阴影,空心,图文混排
        // 创建一个描述文本属性的字典
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        attrs[NSForegroundColorAttributeName] = TITLECOLOR;
        [item setTitleTextAttributes:attrs forState:UIControlStateSelected];
//        attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
//        [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
        
        // 设置字体尺寸:只有设置正常状态下,才会有效果
        NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
        attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
        [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    // 1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构
    [self setupAllChildViewController];
    
    // 2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性
    [self setupAllTitleButton];
    
    [self.tabBar setShadowImage:[[UIImage alloc]init]];

    
}
#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
    // 首页
    HomeViewController *homeVc = [[HomeViewController alloc] init];
    SJNavgationController *nav = [[SJNavgationController alloc] initWithRootViewController:homeVc];
    [self addChildViewController:nav];
    
    // 工单
    WorkOrderViewController *workOrderVc = [[WorkOrderViewController alloc] init];
    SJNavgationController *nav1 = [[SJNavgationController alloc] initWithRootViewController:workOrderVc];
    [self addChildViewController:nav1];
    
    
    if ([[UserInfoTool shareUserInfoTool].Info.Role isEqualToString:@"030003001"]) {
        // 管理中心
        ControlCenterTableVC *controlCenterVc = [[ControlCenterTableVC alloc] init];
        SJNavgationController *nav3 = [[SJNavgationController alloc] initWithRootViewController:controlCenterVc];
        [self addChildViewController:nav3];
    }
    
    // 个人中心
    MineViewController *mineVc = [[MineViewController alloc] init];
    SJNavgationController *nav4 = [[SJNavgationController alloc] initWithRootViewController:mineVc];
    [self addChildViewController:nav4];
}

// 设置tabBar上所有按钮内容
- (void)setupAllTitleButton
{
    // 0:首页
    UINavigationController *nav = self.childViewControllers[0];
    nav.tabBarItem.title = @"嗨回收";
    nav.tabBarItem.image = [UIImage imageOriginalWithName:@"icon_index_unselected"];
    // 快速生成一个没有渲染图片
    nav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_index_selected"];
    
    // 1:我的工单
    UINavigationController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"我的工单";
    nav1.tabBarItem.image = [UIImage imageOriginalWithName:@"icon_workorder_unselected"];
    nav1.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_workorder_selected"];
    
    if ([[UserInfoTool shareUserInfoTool].Info.Role  isEqualToString:@"030003001"]) {
        // 3.管理中心
        UINavigationController *nav3 = self.childViewControllers[2];
        nav3.tabBarItem.title = @"管理中心";
        nav3.tabBarItem.image = [UIImage imageOriginalWithName:@"icon_admin_unselected"];
        nav3.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_admin_selected"];
    }
    
    NSInteger index = ([[UserInfoTool shareUserInfoTool].Info.Role  isEqualToString:@"030003001"])?3:2;
    // 4.个人中心
    UINavigationController *nav4 = self.childViewControllers[index];
    nav4.tabBarItem.title = @"个人中心";
    nav4.tabBarItem.image = [UIImage imageOriginalWithName:@"icon_personcenter_unselected"];
    nav4.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_personcenter_selected"];
}


@end
